﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBManager.Admin;
using BO.Admin;
using System.Data;

namespace ProjectQuaDep.Admin.Products
{
    public partial class EmployeeManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowAll();
            }

        }
        protected void ShowAll()
        {
            string checkStatus = rblStatus.SelectedValue.ToString();
            DataTable dt = EmployeeBO.GetAllEmployeeAdminBO(txtSearch.Text.Trim(), checkStatus);
            gvShow.DataSource = dt;
            gvShow.DataBind();
        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            string Name = txtName.Text.Trim();
            string Email = txtEmail.Text.Trim();
            string Tel = txtTel.Text.Trim();
            string Address = txtAddress.Text.Trim();
            string Birthday = DateTime.Now.ToString("yyyyMMdd");
            string Descriptions = "";
            string InfomationOther = "";
            string Status = BO.Utils.getOneSelect(ckSelect);
            string Sex = "1";
            string ID = lblID.Text;
            if (cmdUpdate.Text == "Thêm Mới")
            {
                //them moi
                int kq = EmployeeBO.AddEmployeeBO(Name, Email, Tel, Address, Birthday, Descriptions, InfomationOther, Status, Sex);
                if (kq == 1)
                {
                    lblMs.Text = "";
                    pnUpdate.Visible = false;
                    ShowAll();
                }
                else
                {
                    lblMs.Text = "Thêm mới thất bại";
                }
            }
            else
            {
                //sua chua
                int kq = EmployeeBO.EditEmployeeBO(Name, Email, Tel, Address, Birthday, Descriptions, InfomationOther, Status, Sex, ID);
                if (kq == 1)
                {
                    lblMs.Text = "";
                    pnUpdate.Visible = false;
                    ShowAll();
                }
                else
                {
                    lblMs.Text = "Sửa chữa thất bại";
                }
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            ShowAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                pnUpdate.Visible = true;
                DataTable dt = BO.Admin.EmployeeBO.GetInfomationByIDBO(ID);
                if (dt.Rows.Count > 0)
                {
                    cmdUpdate.Text = "Sửa Chữa";
                    pnUpdate.Visible = true;
                    lblID.Text = ID;
                    txtAddress.Text = dt.Rows[0][BO.Const.tbEmployee.Address].ToString();
                    txtEmail.Text = dt.Rows[0][BO.Const.tbEmployee.Email].ToString();
                    txtName.Text = dt.Rows[0][BO.Const.tbEmployee.Name].ToString();
                    txtTel.Text = dt.Rows[0][BO.Const.tbEmployee.Tel].ToString();
                    BO.Utils.setOneSelect(ckSelect, dt.Rows[0][BO.Const.tbEmployee.Status].ToString());
                    BO.Utils.setOneSelect(ckSelect, dt.Rows[0][BO.Const.tbCompanies.Status].ToString());
                }
            }
            else if (cmdName == "cmdRemove")
            {
                EmployeeBO.EditStatusEmployeeBO(ID, "0");
                ShowAll();
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            ShowAll();
        }

        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            lblID.Text = "";
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtName.Text = "";
            txtTel.Text = "";
            cmdUpdate.Text = "Thêm Mới";
            pnUpdate.Visible = true;
        }

        protected void rblStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAll();
        }
    }
}